<?php 
class Myblog_Controller_Action_Helper_Jqgrid extends Zend_Controller_Action_Helper_Abstract
{
private $_request,$_dbName,$_json,$_page,$_limit,$_count,$_data,$_totalPages,$_sidx,$_sord,$_item,$_render;

	public function __construct()
	{
	$this->_json=new Zend_Controller_Action_Helper_Json();

			if($this->getRequest()->isPost()){
					$obj = new stdClass();
					$obj->request		=	$this->getRequest()->getPost();
					$obj->module		=	$this->getRequest()->getModuleName();
					$obj->controller=	$this->getRequest()->getControllerName();
					$obj->action		=	$this->getRequest()->getActionName();
					$obj->limit			=	100;

						if($obj->controller==='users' && $obj->action==='index'){
								$obj->dbname='Myblog_Model_Users';
								$obj->data = Myblog_Model_Users::getUsers();
								$obj->item='id';
						}
						if($obj->controller==='category' && $obj->action==='index'){
								$obj->dbname='Myblog_Model_Categories';
								$obj->data = Myblog_Model_Categories::getCategories();
								$obj->item='category_id';
						}
						if($obj->controller==='item' && $obj->action==='index'){

								$obj->dbname='Myblog_Model_Articles';
								$obj->data = Myblog_Model_Articles::getArticles();
								$obj->item='article_id';
						}

					$this->startProcess($obj);
			}
	}
	public function init()
	{
		if($this->getRequest()->isXmlHttpRequest() && !empty($obj->request)){
			$this->_helper->getHelper('layout')->disableLayout();
		}

	}
	public function startProcess(stdClass $obj)
	{
			if(!empty($obj->dbname) && !empty($obj->request))
			{
				$this->_dbName = $obj->dbname;
				$this->_request= $obj->request;
				$this->_item=$obj->item;
				$this->_render=$obj->controller;
                if(isset($obj->request['sord'])){
                    $this->_sord = $obj->request['sord'];
                }
                if(isset($obj->request['sidx'])){
                    $this->_sidx =$obj->request['sidx'];
                }
                if(isset($obj->request['page'])){
                    $this->_page =$obj->request['page'];
                }
				$this->_data = $obj->data;
				$this->_limit= $obj->limit;
				$this->_count= count($obj->data);

				if($this->_count > 0){
					 $this->_totalPages = ceil($this->_count/$this->_limit);}
				else{$this->_page = 0;}
				
				if($this->_page > $this->_totalPages){
					 $this->_page = $this->_totalPages;}

				$this->preparing();
				
			}else{throw new Zend_Controller_Action_Exception('Invalid input in '.__FUNCTION__);}
	}
	
	private function preparing()
	{
		if(isset($this->_request['oper'])){
			$oper = $this->_request['oper'];	
			$this->handler($oper);
		}
		call_user_func(array($this,$this->_render));
	}
	private function users()
	{
		if($this->_data){
						for($i=0;$i<$this->_count;$i++){
							$responce->rows[$i]['id']=$this->_data[$i]['id'];
							$responce->rows[$i]['cell']=array($this->_data[$i]['id'],$this->_data[$i]['avatar'],$this->_data[$i]['userlogin'],$this->_data[$i]['username'],$this->_data[$i]['email'],
							$this->_data[$i]['role'],$this->_data[$i]['reg_time'],$this->_data[$i]['password']);
							}
							$response = array('page' =>$this->_page,'total' => $this->_totalPages, 'records' => $this->_count, 'rows' => $responce->rows);
							$this->_json->sendJson($response);
		}else{throw new Zend_Controller_Action_Exception('Invalid input in '.__FUNCTION__);}
	}
	private function item()
	{
	if($this->_data){
						for($i=0;$i<$this->_count;$i++) {
							$responce->rows[$i]['id']=$this->_data[$i][$this->_item];
							$responce->rows[$i]['cell']=array($this->_data[$i][$this->_item],$this->_data[$i]['fileToUpload'],$this->_data[$i]["title"],$this->_data[$i]["category_name"],$this->_data[$i]["author"],
							$this->_data[$i]["page"],$this->_data[$i]["created_dt"],$this->_data[$i]['visibility'],$this->_data[$i]["comments"],$this->_data[$i]["article"]);
							}
							$response = array('page' => $this->_page, 'total' => $this->_totalPages, 'records' => $this->_count, 'rows' => $responce->rows);
    					$this->_json->sendJson($response);
		}

    //else{throw new Zend_Controller_Action_Exception('Invalid input in '.__FUNCTION__);}
	}
	private function category()
	{
    if($this->_data){
		for($i=0;$i<$this->_count;$i++){
						$responce->rows[$i]['id']=$this->_data[$i][$this->_item];
						$responce->rows[$i]['cell']=array($this->_data[$i][$this->_item],$this->_data[$i]['category_name']);
						}
						$response = array('page' => $this->_page, 'total' =>$this->_totalPages ,'records' => $this->_count, 'rows' => $responce->rows);
						$this->_json->sendJson($response);
    }else{throw new Zend_Controller_Action_Exception('Invalid input in '.__FUNCTION__);}
  }

	private function handler($oper){

		if($this->_request['oper']===$oper && !empty($this->_dbName) && !empty($this->_request) && !empty($this->_item)){
			if($oper==='add'){$oper='adding';}
						$operation = call_user_func_array(array($this->_dbName,$oper),array('0'=>$this->_request,'1'=>$this->_dbName,'2'=>$this->_item));
						$this->_json->sendJson(array("oper"=>$operation));
		}
	}						
}
